#ifndef _NGX_MODULE_CONFIG_H_
#define _NGX_MODULE_CONFIG_H_
#include "NgxInclude.hpp"


/// @brief 
/// @tparam T3 loc location层次
/// @tparam T2 srv server层次
/// @tparam T1 main http层次
template<typename T3 = void, typename T2 = void, typename T1 = void>
class NgxModuleConfig final
{
public:
	NgxModuleConfig(ngx_uint_t idx) : idx_(idx) 	//获取模块的序号
	{}
	~NgxModuleConfig() = default;
private:
	ngx_uint_t idx_;
public:
	ngx_uint_t index() const
	{
		return idx_;
	}

	template<typename T, typename U>
	T* get(U conf) const
	{
		return reinterpret_cast<T*>(conf[index()]);
	}
private:
	ngx_http_conf_ctx_t *ctx(ngx_conf_t *cf) const
	{
		return reinterpret_cast<ngx_http_conf_ctx_t*>(cf->ctx);
	}

/* 获取 ngx_conf_t 的配置信息 */
public:
	T1 *main(ngx_conf_t *cf) const	// 获取http层次的配置信息
	{
		return get<T1*>(ctx(cf)->main_conf);
	}

	T2 *srv(ngx_conf_t *cf) const	// 获取server层次的配置信息
	{
		return get<T2*>(ctx(cf)->srv_conf);
	}

	T3 *loc(ngx_conf_t *cf) const	// 获取location层次的配置信息
	{
		return get<T3*>(ctx(cf)->loc_conf);
	}


/* 获取 ngx_http_request_t 的配置信息 */
public:		
	T1* main(ngx_http_request_t* r) const
	{
		return get<T1>(r->main_conf);
	}

	T2* srv(ngx_http_request_t* r) const
	{
		return get<T2>(r->srv_conf);
	}

	T3* loc(ngx_http_request_t* r) const
	{
		return get<T3>(r->loc_conf);
	}
};


#endif //!_NGX_MODULE_CONFIG_H_